# -*- coding: utf-8 -*-


import json

#解析json
def parse_json(filename):
    def strip_comment(line):
        character_end = True
        character_len = len(line)
        for i,x in enumerate(line):
                if x == '"': character_end = not character_end
                if character_end and x == '/' and i < character_len-2 and line[i+1] == '/':
                        return line[:i] + "\n"
        return line
    file = open(filename, encoding = "utf8")
    res = [strip_comment(line) for line in file.readlines()]
    result = []
    comment = False
    for l in res:
        if l.find("/*") != -1: comment = True
        if not comment: result.append(l)
        if l.find("*/") != -1: comment = False
    str_res = "".join(result)
    return json.loads(str_res)


# 读取项目配置
def reading_project_configuration( file_path = "项目配置.json"):
    project = parse_json(file_path)

    关键字集合= { "masterip", "masterport", "private_key_file", "mod_path" }
    if project and type(project) == dict and set(project) >= 关键字集合:
        return project
    return {} 

# 解析请求命令
def parse_request_command(file_path):
    request = parse_json(file_path)
    # with open("engineRequest.json",   encoding="UTF-8" ) as file:
    #     self.engine_request = json.load(file)

    总请求数量 = 0
 
    请求字典 = { }

    关键字集合= { "desc", "classify", "searchKey", "defaultParam", "paramDesc" }
    if request and type(request) == dict:
        总请求数量 = len(request)
        for comm, para in request.items():
            if type(para)==dict and set(para) >= 关键字集合:
               请求字典[comm] = para
  
    return 总请求数量, 请求字典
